package com.cosmoplat.xikai.business.controller.mes;

import com.cosmoplat.xikai.business.domain.dto.mes.CloseAlarmByCheckedMesDTO;
import com.cosmoplat.xikai.business.domain.dto.mes.MaterialUpdateByMesDTO;
import com.cosmoplat.xikai.business.domain.dto.mes.ProductionPlanByMesDTO;
import com.cosmoplat.xikai.business.service.MesCallService;
import com.cosmoplat.xikai.common.annotation.MesPermission;
import com.cosmoplat.xikai.common.core.domain.XikaiMesResult;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName MesCallController
 * @Description mes用于回调的接口
 * @Author wdf
 * @Date 2024/12/26
 */
@Api(tags = "mes调用接口", value = "mes调用接口")
@RestController
@Slf4j
@RequestMapping("/mes")
public class MesCallController {

    @Autowired
    private MesCallService mesCallService;

    @ApiOperation("mes调用测试接口")
    @PostMapping("/test")
    @MesPermission
    public XikaiMesResult call() {
        log.info("mes回调接口-- test");
        return XikaiMesResult.success("调用成功了");
    }

    /**
     * 更新物料信息
     * @date 14:05 2025/1/7
     * @return com.cosmoplat.xikai.common.core.domain.MesResult
     **/
    @ApiOperation("更新物料信息")
    @PostMapping("/updateMaterial")
    @MesPermission
    public XikaiMesResult updateMaterial(@RequestBody @Validated MaterialUpdateByMesDTO material) {
        mesCallService.updateMaterial(material);
        return XikaiMesResult.success("成功");
    }

    /**
     * 下发生产计划
     * @date 14:52 2025/1/7
     * @param productionPlan
     * @return com.cosmoplat.xikai.common.core.domain.MesResult
     **/
    @ApiOperation("下发生产计划")
    @PostMapping("/productionPlan")
    @MesPermission
    public XikaiMesResult productionPlan(@RequestBody @Validated ProductionPlanByMesDTO productionPlan) {
        mesCallService.productionPlan(productionPlan);
        return XikaiMesResult.success("成功");
    }

    /**
     * 点检完成关闭告警
     * @date 14:47 2025/1/7
     * @param closeAlarmByChecked
     * @return com.cosmoplat.xikai.common.core.domain.MesResult
     **/
    @ApiOperation("点检完成关闭告警")
    @PostMapping("/closeAlarmByChecked")
    @MesPermission
    public XikaiMesResult closeAlarmByChecked(@RequestBody @Validated CloseAlarmByCheckedMesDTO closeAlarmByChecked) throws ModbusInitException, ModbusTransportException {
        mesCallService.closeAlarmByChecked(closeAlarmByChecked);
        return XikaiMesResult.success("成功");
    }

}
